import { GoodsRules } from './goodsRule';
let _GoodsRules = GoodsRules;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import GoodsRuleMock from './mk/mng/goods/goodsRuleMock';
 * GoodsRuleMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mk/mng/goods/goodsRule/listKey').reply(config => {
      let params = config.params;
      let mockGoodsRules = _GoodsRules.filter(goodsRule => {
      	if(params.goodsId||params.goodsId==''){ if( goodsRule.goodsId.indexOf(params.goodsId)>=0) return true;}
      	if(params.goodsName||params.goodsName==''){ if( goodsRule.goodsName.indexOf(params.goodsName)>=0) return true;}
      	if(params.isMk||params.isMk==''){ if( goodsRule.isMk.indexOf(params.isMk)>=0) return true;}
      	if(params.mkRule||params.mkRule==''){ if( goodsRule.mkRule.indexOf(params.mkRule)>=0) return true;}
      	if(params.mkPct||params.mkPct==''){ if( goodsRule.mkPct.indexOf(params.mkPct)>=0) return true;}
      	if(params.mkFee||params.mkFee==''){ if( goodsRule.mkFee.indexOf(params.mkFee)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockGoodsRules
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mk/mng/goods/goodsRule/list').reply(config => {
      let params = config.params;
      let mockGoodsRules = _GoodsRules.filter(goodsRule => {
      	var isTrue=false;
      	if(params.goodsId||params.goodsId==''){if(goodsRule.goodsId.indexOf(params.goodsId)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.goodsName||params.goodsName==''){if(goodsRule.goodsName.indexOf(params.goodsName)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isMk||params.isMk==''){if(goodsRule.isMk.indexOf(params.isMk)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mkRule||params.mkRule==''){if(goodsRule.mkRule.indexOf(params.mkRule)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mkPct||params.mkPct==''){if(goodsRule.mkPct.indexOf(params.mkPct)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.mkFee||params.mkFee==''){if(goodsRule.mkFee.indexOf(params.mkFee)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockGoodsRules
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockGoodsRules.length;
      mockGoodsRules = mockGoodsRules.filter((goodsRule, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockGoodsRules,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mk/mng/goods/goodsRule/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  goodsId  } = params;
      _GoodsRules = _GoodsRules.filter(goodsRule => goodsRule.goodsId !== goodsId);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mk/mng/goods/goodsRule/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _GoodsRules = _GoodsRules.filter(goodsRule => !params.includes(goodsRule.goodsId));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mk/mng/goods/goodsRule/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { goodsId,goodsName,isMk,mkRule,mkPct,mkFee } = params;
      _GoodsRules.some(goodsRule => {
        if (goodsRule.goodsId === goodsId) {
			goodsRule.goodsId = goodsId;
			goodsRule.goodsName = goodsName;
			goodsRule.isMk = isMk;
			goodsRule.mkRule = mkRule;
			goodsRule.mkPct = mkPct;
			goodsRule.mkFee = mkFee
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mk/mng/goods/goodsRule/add').reply(config => {
      var params = JSON.parse( config.data );
      let { goodsId,goodsName,isMk,mkRule,mkPct,mkFee } = params;
      _GoodsRules.push({
		goodsId:goodsId,
		goodsName:goodsName,
		isMk:isMk,
		mkRule:mkRule,
		mkPct:mkPct,
		mkFee:mkFee
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};